package com.example.server.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContentProcessor {

    public static String processContent(String messageContent, String aigcFeedback) {
        String sentencesPart = messageContent.split("不同则返回编号-0：")[1].trim();
        String aigcContent = aigcFeedback.split("\"content\":\"")[1].split("\"")[0];

        String[] sentences = sentencesPart.split("\\d+\\. ");
        String[] results = aigcContent.split(", ");
        Map<Integer, String> feedbackMap = new HashMap<>();
        for (String result : results) {
            String[] parts = result.split("-");
            feedbackMap.put(Integer.parseInt(parts[0]), parts[1]);
        }

        StringBuilder processedContent = new StringBuilder();
        Pattern pattern = Pattern.compile("“([^”]+)”");

        for (int i = 1; i < sentences.length; i++) {
            String sentence = sentences[i].trim();
            if (feedbackMap.containsKey(i)) {
                String feedback = feedbackMap.get(i);
                Matcher matcher = pattern.matcher(sentence);
                if (matcher.find()) {
                    String matchedWord = matcher.group(1);
                    if ("1".equals(feedback)) {
                        String stars = "*".repeat(matchedWord.length());
                        sentence = sentence.replace("“" + matchedWord + "”", stars);
                    } else {
                        sentence = sentence.replace("“" + matchedWord + "”", matchedWord);
                    }
                }
            }
            processedContent.append(i).append(". ").append(sentence).append(" ");
        }

        System.out.println(processedContent.toString());
        return processedContent.toString().trim();
    }
}
